package com.rf.richfitwheel.config;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.deser.std.StringDeserializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

@Configuration
@AutoConfigureBefore(JacksonAutoConfiguration.class)
public class DynCommonConfig {

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");

    @Bean
    public ObjectMapper serializingObjectMapper() {
        JavaTimeModule module = new JavaTimeModule();
        LocalDateTimeDeserializer dateTimeDeserializer = new LocalDateTimeDeserializer(DATE_TIME_FORMATTER);
        LocalDateTimeSerializer dateTimeSerializer = new LocalDateTimeSerializer(DATE_TIME_FORMATTER);
        module.addDeserializer(LocalDateTime.class, dateTimeDeserializer);
        module.addSerializer(LocalDateTime.class, dateTimeSerializer);
        LocalDateDeserializer localDateDeserializer = new LocalDateDeserializer(DATE_FORMATTER);
        LocalDateSerializer localDateSerializer = new LocalDateSerializer(DATE_FORMATTER);
        module.addDeserializer(LocalDate.class, localDateDeserializer);
        module.addSerializer(LocalDate.class, localDateSerializer);
        LocalTimeDeserializer localTimeDeserializer = new LocalTimeDeserializer(TIME_FORMATTER);
        LocalTimeSerializer localTimeSerializer = new LocalTimeSerializer(TIME_FORMATTER);
        module.addDeserializer(LocalTime.class, localTimeDeserializer);
        module.addSerializer(LocalTime.class, localTimeSerializer);
        //空串反序列化为null
        module.addDeserializer(String.class, new MyStdDeserializer());
        return Jackson2ObjectMapperBuilder.json().modules(module)
                .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).build();
    }

    private static class MyStdDeserializer extends StdDeserializer<String> {
        public MyStdDeserializer() {
            super(String.class);
        }

        @Override
        public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
            String result = StringDeserializer.instance.deserialize(p, ctxt);
            if (StringUtils.isEmpty(result)) {
                return null;
            }
            return result;
        }
    }
}
